import 'dart:async';

import 'package:bot_toast/bot_toast.dart';
import 'package:flutter/material.dart';
import 'package:shopping_group/baseblocs/bloc_provider.dart';
import 'package:shopping_group/event/event.dart';
import 'package:shopping_group/http/apis/apis.dart';
import 'package:shopping_group/http/http_request_all.dart';
import 'package:shopping_group/modle/login/entity/sv_entity.dart';
import 'package:shopping_group/modle/more/entity/group_fulipush_finish_entity.dart';
import 'package:shopping_group/modle/more/ui/fl_ok_page.dart';
import 'package:shopping_group/utils/log_util.dart';
import 'package:shopping_group/utils/network_utils.dart';
import 'package:shopping_group/utils/route_util.dart';

class VideoBloc implements BlocBase {
  StreamController<Object> _initController = StreamController<Object>();
  StreamSink get initDate => _initController.sink;

  StreamController<Object> _initFlController = StreamController<Object>();
  StreamSink get initFlDate => _initFlController.sink;

  StreamController<Object> _initPlayController = StreamController<Object>();
  StreamSink get initPlayDate => _initPlayController.sink;

  VideoBloc() {
    _initController.stream.listen(initData);
    _initFlController.stream.listen(initFlData);
    _initPlayController.stream.listen(initPlayData);
  }
  void initFlData(data) {
    if (data is Map) {
      _initFlTask(data);
    }
  }

  void initData(data) {
    if (data is Map) {
      _initTask(data);
    }
  }

  void initPlayData(data) {
    if (data is Map) {
      _initPlayTask(data);
    }
  }

  void _initFlTask(data) async {
    FocusScope.of(RouteUtil.navigatorKey.currentContext)
        .requestFocus(FocusNode());
    BotToast.showLoading();
    NetworkUtils.isNetworkConnected(connectedCallback: (connected) {
      if (!connected) {
        BotToast.showText(text: '网络无法连接');
      } else {
        HttpUtilAll.post(Apis.PLAY_FLVIDEO_URL, data: data, headers: {},
            success: (result) {
          BotToast.closeAllLoading();
          if (null != result) {
            GroupFuliPushFinishEntity entity =
                GroupFuliPushFinishEntity.fromJson(result);
            // LogUtil.e(entity.val,
            //     tag: '视频播放结束..................//////////////////');
            eventBus.fire(RefreshFlEvent());
            // RouteUtil.closeView();
            RouteUtil.goParameter(FuliPlayokPage(
              entity: entity,
            ));
          }
          //  LogUtil.e(result.toString(), tag: 'result ////////////////视频播放结束');
        }, error: (errorMsg) {
          BotToast.closeAllLoading();
          RouteUtil.closeView();
          // 请求失败返回的错误信息
          BotToast.showText(text: errorMsg);
        });
      }
    });
  }

  void _initPlayTask(data) async {
    FocusScope.of(RouteUtil.navigatorKey.currentContext)
        .requestFocus(FocusNode());
    BotToast.showLoading();
    NetworkUtils.isNetworkConnected(connectedCallback: (connected) {
      if (!connected) {
        BotToast.showText(text: '网络无法连接');
      } else {
        HttpUtilAll.post(Apis.PLAY_VIDEO_URL, data: data, headers: {},
            success: (result) {
          BotToast.closeAllLoading();
          if (null != result) {
            // SvEntity entity = SvEntity.fromJson(result);
            // LogUtil.e(entity.val,
            //     tag: '视频播放结束..................//////////////////');
            eventBus.fire(XiYuAvCloseEvent());

            // RouteUtil.closeView();
          }
          LogUtil.e(result.toString(), tag: 'result ////////////////视频播放结束');
        }, error: (errorMsg) {
          BotToast.closeAllLoading();
          RouteUtil.closeView();
          // 请求失败返回的错误信息
          BotToast.showText(text: errorMsg);
        });
      }
    });
  }

  void _initTask(data) async {
    FocusScope.of(RouteUtil.navigatorKey.currentContext)
        .requestFocus(FocusNode());
    BotToast.showLoading();
    NetworkUtils.isNetworkConnected(connectedCallback: (connected) {
      if (!connected) {
        BotToast.showText(text: '网络无法连接');
      } else {
        HttpUtilAll.post(Apis.PLAY_VIDEO_URL, data: data, headers: {},
            success: (result) {
          BotToast.closeAllLoading();
          if (null != result) {
            // SvEntity entity = SvEntity.fromJson(result);
            // LogUtil.e(entity.val,
            //     tag: '视频播放结束..................//////////////////');
            eventBus.fire(RefreshEvent());
           RouteUtil.closeView();
          }
          LogUtil.e(result.toString(), tag: 'result ////////////////视频播放结束');
        }, error: (errorMsg) {
          BotToast.closeAllLoading();
          RouteUtil.closeView();
          // 请求失败返回的错误信息
          BotToast.showText(text: errorMsg);
        });
      }
    });
  }

  @override
  void dispose() {
    _initController?.close();
    _initFlController?.close();
    _initPlayController?.close();
  }
}
